package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.GroupMessageDao;
import com.zja.model.GroupMessage;

import java.util.List;

/**
 * @author JA
 */
public class GroupMessageDaoImpl extends BaseDAO implements GroupMessageDao {
    @Override
    public List<GroupMessage> selectAll() {
        String sql = "SELECT * FROM group_message";
        try {
            return executeQuery(GroupMessage.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public GroupMessage selectById(Long id) {
        String sql = "SELECT * FROM group_message WHERE id = ?";
        try {
            return executeQueryBean(GroupMessage.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(GroupMessage groupMessage) {
        String sql = "INSERT INTO group_message(group_id, sender_id, content, created_at) VALUES(?, ?, ?, ?)";
        try {
            return executeUpdate(sql, groupMessage.getGroupId(), groupMessage.getSenderId(), groupMessage.getContent(), groupMessage.getCreatedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(GroupMessage groupMessage) {
        String sql = "UPDATE group_message SET group_id = ?, sender_id = ?, content = ?, created_at = ? WHERE id = ?";
        try {
            return executeUpdate(sql, groupMessage.getGroupId(), groupMessage.getSenderId(), groupMessage.getContent(), groupMessage.getCreatedAt(), groupMessage.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        String sql = "DELETE FROM group_message WHERE id = ?";
        try {
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
